package com.yanqu.road.server.manger.marriage;

import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;

import java.math.BigDecimal;
import java.math.BigInteger;

public class MarriageConfigMgr extends TempMgr {

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    /**
     * 设置限制赚速时候超过配置
     * @param childEarnSpeed
     * @param limitEarnSpeed
     * @return
     */
    public static boolean isOverMarriageLimitEarnSpeedConfig(BigInteger childEarnSpeed, BigInteger limitEarnSpeed){
        BigDecimal val1 = new BigDecimal(childEarnSpeed);
        BigInteger minEarnSpeed = val1.multiply(BigDecimal.valueOf(GameConfig.CROSS_MARRIAGE_LIMIT_MIN_EARNSPEED)).
                divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).toBigInteger();
        minEarnSpeed = getSubBigInteger(minEarnSpeed);
        BigInteger maxEarnSpeed = val1.multiply(BigDecimal.valueOf(GameConfig.CROSS_MARRIAGE_LIMIT_MAX_EARNSPEED)).
                divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).toBigInteger();
        maxEarnSpeed = getSubBigInteger(maxEarnSpeed);
        return limitEarnSpeed.compareTo(minEarnSpeed) < 0 || limitEarnSpeed.compareTo(maxEarnSpeed) > 0;
    }

    /**
     * 取前4位整数
     * @param value
     * @return
     */
    private static BigInteger getSubBigInteger(BigInteger value){
        String valStr = value.toString();
        int length = valStr.length();
        String newValStr;
        if(length > 4){
            newValStr = "";
            for(int i = 0; i < 4; i++){
                newValStr += valStr.charAt(i);
            }
            for(int i = length - 4; i > 0; i--){
                newValStr += "0";
            }
        }else {
            newValStr = valStr;
        }
        return new BigInteger(newValStr);
    }
}
